# RUN: llc %s -o - -mtriple=x86_64-unknown-unknown \
# RUN:    -experimental-debug-variable-locations -run-pass=livedebugvalues\
# RUN:    | FileCheck %s
#
# Test that a DBG_INSTR_REF that refers to a DBG_PHI, will be translated into a
# DBG_VALUE of the value read at that DBG_PHI -- in this test, when the value
# is on the stack.
# 
--- |
  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
  target triple = "x86_64-unknown-linux-gnu"
  
  define dso_local i32 @foo(i64 %bar, i64 %baz) !dbg !7 {
    ret i32 0
  }
  
  declare dso_local void @ext(i64)
  
  declare dso_local i64 @getlong()
  
  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!3, !4, !5}
  !llvm.ident = !{!6}
  
  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
  !1 = !DIFile(filename: "test.c", directory: "/tmp/out.c")
  !2 = !{}
  !3 = !{i32 7, !"Dwarf Version", i32 4}
  !4 = !{i32 2, !"Debug Info Version", i32 3}
  !5 = !{i32 1, !"wchar_size", i32 4}
  !6 = !{!""}
  !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
  !8 = !DISubroutineType(types: !9)
  !9 = !{!10, !11, !11}
  !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !11 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
  !12 = !DILocalVariable(name: "bar", arg: 1, scope: !7, file: !1, line: 3, type: !11)
  !13 = !DILocation(line: 0, scope: !7)
  !14 = !DILocalVariable(name: "baz", arg: 2, scope: !7, file: !1, line: 3, type: !11)
  !19 = distinct !DILexicalBlock(scope: !7, file: !1, line: 8, column: 7)
  !26 = !DILocation(line: 13, column: 3, scope: !7)

...
---
name:            foo
alignment:       16
tracksRegLiveness: true
debugInstrRef: true
liveins:
  - { reg: '$rdi' }
  - { reg: '$rsi' }
frameInfo:
  stackSize:       24
  offsetAdjustment: -24
  maxAlignment:    1
  adjustsStack:    true
  hasCalls:        true
  maxCallFrameSize: 0
  cvBytesOfCalleeSavedRegisters: 16
fixedStack:
  - { id: 0, type: spill-slot, offset: -24, size: 8, alignment: 8, callee-saved-register: '$rbx' }
  - { id: 1, type: spill-slot, offset: -16, size: 8, alignment: 16, callee-saved-register: '$r14' }
machineFunctionInfo: {}
stack:
  - { id: 0, name: '', type: spill-slot, offset: 0, size: 8, alignment: 8,
      stack-id: default, callee-saved-register: '', callee-saved-restored: true,
      debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }

body:             |
  bb.0:
    liveins: $rdi, $rsi, $r14, $rbx
    ; CHECK-LABEL: bb.0:
  
    $r14 = MOV64rr $rsi
    $rbx = MOV64rr $rdi
    $rax = MOV64ri 0
    MOV64mr $rsp, 1, $noreg, 16, $noreg, $rax :: (store 8 into %stack.0)
    $rax = MOV64ri 0

    ;; A DBG_PHI of the stack should resolve to the stack location.
    DBG_PHI %stack.0, 1, 64

    ;; Reload value, clobber stack location.
    $rcx = MOV64rm $rsp, 1, $noreg, 8, $noreg :: (load 8 from %stack.0)
    MOV64mr $rsp, 1, $noreg, 16, $noreg, $rax :: (store 8 into %stack.0)

    ;; This should resolve to the loaded register.
    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0), debug-location !13
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(1, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}} $rcx

    ;; And if we say it's a smaller size, we should be able to pick out smaller
    ;; subregisters within the stack slot.
    DBG_PHI %stack.0, 2, 32
    $rax = MOV64ri 0
    $rcx = MOV64rm $rsp, 1, $noreg, 8, $noreg :: (load 8 from %stack.0)
    MOV64mr $rsp, 1, $noreg, 16, $noreg, $rax :: (store 8 into %stack.0)

    ;; This should pick out the 32 bit value.
    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(2, 0), debug-location !13
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(2, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}} $ecx

    ;; Try all the other subregs.
    DBG_PHI %stack.0, 3, 16
    $rax = MOV64ri 0
    $rcx = MOV64rm $rsp, 1, $noreg, 8, $noreg :: (load 8 from %stack.0)
    MOV64mr $rsp, 1, $noreg, 16, $noreg, $rax :: (store 8 into %stack.0)

    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(3, 0), debug-location !13
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(3, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}} $cx

    DBG_PHI %stack.0, 4, 8
    $rax = MOV64ri 0
    $rcx = MOV64rm $rsp, 1, $noreg, 8, $noreg :: (load 8 from %stack.0)
    MOV64mr $rsp, 1, $noreg, 16, $noreg, $rax :: (store 8 into %stack.0)

    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(4, 0), debug-location !13
    ; CHECK:      DBG_INSTR_REF {{.+}}, dbg-instr-ref(4, 0)
    ; CHECK-NEXT: DBG_VALUE_LIST {{.+}} $cl

    ;; We can't, at this time, describe subregister fields with nonzero offset.
    ;; It's easily achieved by attaching more data to stack DBG_PHIs, but it's
    ;; not clear that LLVM will ever merge registers to generate such locations.

    RET64 implicit $eax, debug-location !26

...
